import router from './router'
import store from './store'
import { menu } from '@/utils/menu'
import {
    Message
} from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import {
    getToken
} from '@/utils/auth' // get token from cookie
import getPageTitle from '@/utils/get-page-title'

NProgress.configure({
    showSpinner: false
}) // NProgress Configuration

const whiteList = ['/login'] // no redirect whitelist

let arr = [
    {
        "id": 1,
        "name": "index",
        "title": "首页",
        "pid": 0,
        "path": "/", "child": []
    }
    , {
        "id": 2,
        "name": "systemManagement",
        "title": "系统管理",
        "pid": 0,
        "path": "/systemManagement",
        "child": [
            { "id": 3, "name": "corporateArchitecture", "title": "公司架构", "pid": 2, "path": "corporateArchitecture", "child": [] },
            { "id": 4, "name": "staffManagement", "title": "员工管理", "pid": 2, "path": "staffManagement", "child": [] },
            { "id": 5, "name": "authorityManagement", "title": "岗位权限管理", "pid": 2, "path": "authorityManagement", "child": [
                { 
                    child: [],
                    id: 13,
                    name: "jurisdiction",
                    path: "jurisdiction",
                    pid: 2,
                    title: "岗位权限",
                },
            ] },
            { "id": 6, "name": "TypeOfWork", "title": "工作类别", "pid": 2, "path": "TypeOfWork", "child": [] }
        ]
    }, {
        "id": 2,
        "name": "storeManagement",
        "title": "仓库管理",
        "pid": 0,
        "path": "/storeManagement",
        "child": [
            { "id": 3, "name": "acquisitionApplication", "title": "领料申请", "pid": 2, "path": "acquisitionApplication", "child": 
            [
                { 
                    child: [],
                    id: 13,
                    name: "material",
                    path: "material",
                    pid: 2,
                    title: "",
                }, 
                { 
                    child: [],
                    id: 14,
                    name: "materialDetails",
                    path: "materialDetails",
                    pid: 2,
                    title: "",
                }, 
            ] },
            { "id": 6, "name": "putInStorage", "title": "入库", "pid": 2, "path": "putInStorage", "child": [
                { 
                    child: [],
                    id: 19,
                    name: "materialsPut",
                    path: "materialsPut",
                    pid: 2,
                    title: "物料入库",
                },
            ] },
            { "id": 4, "name": "delivery", "title": "出库", "pid": 2, "path": "delivery", "child": [
                { 
                    child: [],
                    id: 19,
                    name: "materialOutbound",
                    path: "materialOutbound",
                    pid: 2,
                    title: "物料出库",
                }, 
            ] },
        ]
    },
    {
        "id": 5, "name": "aboutYDY", "title": "行政管理", "pid": 0, "path": "/aboutYDY", "child": [
            {
                "id": 99,
                "name": "addAdministrative",
                "title": "行政申请",
                "pid": 5,
                "path": "addAdministrative",
                "child": [
                    { 
                        child: [],
                        id: 3,
                        name: "receptionApply",
                        path: "receptionApply",
                        pid: 5,
                        title: "接待申请",
                    },
                ]
            },
            {
                "id": 98,
                "name": "facilitiesMaintenance",
                "title": "设备类维护",
                "pid": 5,
                "path": "facilitiesMaintenance",
                "child": []
            },
            {
                "id": 123,
                "name": "personnelItems",
                "title": "人员物品放行",
                "pid": 5,
                "path": "personnelItems",
                "child": []
            },
        ]
    },
    {
        "id": 3,
        "name": "personnelManagement",
        "title": "人事管理",
        "pid": 0,
        "path": "/personnelManagement",
        "child": [
            { "id": 7, "name": "attendance", "title": "考勤管理", "pid": 3, "path": "attendance", "child": [
                
            ] },
            { "id": 8, "name": "overtime", "title": "加班申请", "pid": 3, "path": "overtime", "child": [
                { 
                    child: [],
                    id: 13,
                    name: "overtimeApply",
                    path: "overtimeApply",
                    pid: 3,
                    title: "加班申请",
                },
            ] },
            { "id": 9, "name": "takeWorking", "title": "调休申请", "pid": 3, "path": "takeWorking", "child": [
                { 
                    child: [],
                    id: 14,
                    name: "takeWorkingApply",
                    path: "takeWorkingApply",
                    pid: 9,
                    title: "",
                },
            ] },
            { "id": 10, "name": "applicationForLeave", "title": "请假申请", "pid": 3, "path": "applicationForLeave", "child": [
                { 
                    child: [],
                    id: 14,
                    name: "travelApply",
                    path: "travelApply",
                    pid: 3,
                    title: "",
                },
            ] },
            { "id": 11, "name": "evection", "title": "出差申请", "pid": 3, "path": "evection", "child": [
                { 
                    child: [],
                    id: 14,
                    name: "jobApply",
                    path: "jobApply",
                    pid: 3,
                    title: "",
                },
            ] },
            { "id": 12, "name": "job", "title": "招聘申请", "pid": 3, "path": "job", "child": [
                { 
                    child: [],
                    id: 14,
                    name: "leaveApply",
                    path: "leaveApply",
                    pid: 9,
                    title: "",
                },
            ] },
            { "id": 13, "name": "record", "title": "人员档案管理", "pid": 3, "path": "record", "child": [] },
            { "id": 14, "name": "grade", "title": "等级管理", "pid": 3, "path": "grade", "child": [] },
            { "id": 15, "name": "personnelInspection", "title": "人员考核", "pid": 3, "path": "personnelInspection", "child": [
                { 
                    child: [],
                    id: 14,
                    name: "inspectionType",
                    path: "inspectionType",
                    pid: 3,
                    title: "",
                },
            ] },
            { "id": 16, "name": "rewards", "title": "奖罚通报", "pid": 3, "path": "rewards", "child": [
                { 
                    child: [],
                    id: 14,
                    name: "addRewards",
                    path: "addRewards",
                    pid: 3,
                    title: "",
                },
            ] },
            { "id": 17, "name": "welfare", "title": "员工晋升福利申请", "pid": 3, "path": "welfare", "child": [
                { 
                    child: [],
                    id: 14,
                    name: "Addwelfare",
                    path: "Addwelfare",
                    pid: 3,
                    title: "",
                },
            ] },
            { "id": 18, "name": "transfer", "title": "入职离职调动", "pid": 3, "path": "transfer", "child": [
                { 
                    child: [],
                    id: 14,
                    name: "addTransfer",
                    path: "addTransfer",
                    pid: 3,
                    title: "",
                },
            ] },
            { "id": 19, "name": "trainingPlan", "title": "培训计划", "pid": 3, "path": "trainingPlan", "child": [
                { 
                    child: [],
                    id: 14,
                    name: "addTrainingPlan",
                    path: "addTrainingPlan",
                    pid: 3,
                    title: "",
                },
            ] },
            { "id": 20, "name": "daily", "title": "日报", "pid": 3, "path": "daily", "child": [] },
            { "id": 21, "name": "weekly", "title": "周报月报年报", "pid": 3, "path": "weekly", "child": [
                { 
                    child: [],
                    id: 14,
                    name: "addWeekly",
                    path: "addWeekly",
                    pid: 3,
                    title: "",
                },
            ] },
        ]
    },]
// let arr = JSON.parse(localStorage.getItem('menu'))
// if(arr){
//     router.options.routes = menu(router.options.routes, arr)
// }
console.log(router.options.routes,"#$%");
router.beforeEach(async (to, from, next) => {
    // start progress bar
    NProgress.start()
    // set page title
    document.title = getPageTitle(to.meta.title)

    // determine whether the user has logged in
    const hasToken = getToken()

    if (hasToken) {
        if (to.path === '/login') {
            // if is logged in, redirect to the home page
            next({
                path: '/index'
            })
            NProgress.done()
        } else {
            next()
        }
        // if (to.path === '/login') {
        //   // if is logged in, redirect to the home page
        //   next({ path: '/' })
        //   NProgress.done()
        // } else {
        //   const hasGetUserInfo = store.getters.name
        //   if (hasGetUserInfo) {
        //     next()
        //   } else {
        //     try {
        //       // get user info
        //       await store.dispatch('user/getInfo')

        //       next()
        //     } catch (error) {
        //       // remove token and go to login page to re-login
        //       await store.dispatch('user/resetToken')
        //       Message.error(error || 'Has Error')
        //       next(`/login?redirect=${to.path}`)
        //       NProgress.done()
        //     }
        //   }
        // }
    } else {
        /* has no token*/
        if (whiteList.indexOf(to.path) !== -1) {
            // in the free login whitelist, go directly
            next()
        } else {
            // other pages that do not have permission to access are redirected to the login page.
            next(`/login`)
            NProgress.done()
        }
    }
})

router.afterEach(() => {
    // finish progress bar
    NProgress.done()
})